package cn.com.nes.site.controller.crm.opportunity.vo;

import cn.com.nes.site.entity.em.crm.customer.EnergyTypeEnum;
import cn.com.nes.site.entity.em.crm.opportunity.*;
import cn.com.nes.site.entity.em.project.ProjectRelationDataTypeEnum;
import cn.com.nes.site.entity.vo.PageVO;
import cn.com.nes.site.service.crm.customer.bo.SaveOpportunityBO;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityBO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityAreaCodeDTO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityDeviceModelDTO;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@ApiModel(value = "商机保存入参模型")
@Data
public class SaveOpportunityVO extends PageVO {

    // ----------- 通用参数 -----------
    @ApiModelProperty(value = "商机ID")
    private String uuid;

    @ApiModelProperty(value = "所属公司")
    private String companyId;

    @ApiModelProperty("商机分类")
    private String opportunityClassification;

    @ApiModelProperty(value = "商机阶段 01-新商机 02-跟进中 03-商机成交（签订合同，生成项目）04-商机流失")
    private String opportunityStage;

    @ApiModelProperty(value = "商机名称", required = true)
    @NotEmpty(message = "商机名称")
    private String opportunityName;

    @ApiModelProperty(value = "所属客户", required = true)
    @NotEmpty(message = "所属客户")
    private String customerId;

    @ApiModelProperty(value = "房屋ID", required = true)
    private String houseId;

    @ApiModelProperty(value = "商机类型：01-设备购买 02-售后维修  03-项目洽谈")
    private String opportunityType;

    @ApiModelProperty(value = "jsonData")
    private String jsonData;

    @ApiModelProperty(value = "模板ID")
    private String templateId;

    @ApiModelProperty(value = "经度")
    private BigDecimal longitude;

    @ApiModelProperty(value = "纬度")
    private BigDecimal latitude;

    @ApiModelProperty(value = "商机金额")
    private BigDecimal opportunityMoney;

    @ApiModelProperty(value = "线索Id")
    private String clueId;

    @ApiModelProperty(value = "备注")
    private String memo;

    @ApiModelProperty(value = "行政区划", required = true)
    @NotBlank(message = "行政区划")
    private String xzqhdm;

    @ApiModelProperty(value = "项目地址")
    private String projectAddress;

    @ApiModelProperty(value = "进场时间")
    private Date mobilizationTime;

    @ApiModelProperty(value = "竣工时间")
    private Date completeTime;

    @ApiModelProperty(value = "订货时间")
    private Date orderGoodsTime;

    @ApiModelProperty(value = "联系人")
    private String personInCharge;

    @ApiModelProperty(value = "联系电话")
    private String personInChargePhone;

    @ApiModelProperty(value = "性别")
    private String sex;

    @ApiModelProperty(value = "采购方采购方式")
    private String cgfType;

    @ApiModelProperty(value = "采购方联系方式")
    private String cgfPhone;

    @ApiModelProperty(value = "设计方公司名")
    private String sjfCompanyName;

    @ApiModelProperty(value = "设计方联系方式")
    private String sjfPhone;

    @ApiModelProperty(value = "承建方公司名")
    private String cjfCompanyName;

    @ApiModelProperty(value = "承建方联系方式")
    private String cjfPhone;

    @ApiModelProperty(value = "投资方公司名")
    private String tzfCompanyName;

    @ApiModelProperty(value = "投资方联系方式")
    private String tzfPhone;

    @ApiModelProperty(value = "监理方公司名")
    private String jlfCompanyName;

    @ApiModelProperty(value = "监理方联系方式")
    private String jlfPhone;

    @ApiModelProperty("设计图")
    private List<String> designDrawings;

    @ApiModelProperty("平面图")
    private List<String> planeGraphs;

    @ApiModelProperty(value = "预期金额")
    private BigDecimal amount;

    @ApiModelProperty(value = "bimId")
    private String bimId;

    @ApiModelProperty(value = "微信")
    private String wechat;

    @ApiModelProperty(value = "预计成交日期")
    private Date estimatedClosingDate;

    /** ----- 补充数据项 ----- */
    @ApiModelProperty(value = "身份证正面照片")
    private String idCardFrontImage;

    @ApiModelProperty(value = "身份证号反面照片")
    private String idCardBackImage;

    @ApiModelProperty("户口本首页照片")
    private String householdRegistrationBookFirstPageImage;

    @ApiModelProperty("户口本户主页照片")
    private String householdRegistrationBookHomePageImage;

    // ----------- 政府商务项目参数 -----------

    @ApiModelProperty("允许的服务区域范围")
    private List<OpportunityAreaCodeDTO> allowAreaCodes;

    @ApiModelProperty(value = "补贴方式", notes = "政府商务（终端客户）项目必须填写，0-按户补贴，1-按平米补贴")
    private String allowanceType;

    @ApiModelProperty(value = "改造类型", notes = "政府商务项目必填，字典类别名称：改造类型，值域对应 t_param 表 param_type=336")
    private RenovationType renovationType;

    @ApiModelProperty(value = "补贴对象", notes = "01-厂家，02-农户")
    private AllowanceTarget allowanceTarget;

    @ApiModelProperty(value = "补贴下限")
    private BigDecimal allowanceDown;

    @ApiModelProperty(value = "补贴上限")
    private BigDecimal allowanceUp;

    @ApiModelProperty(value = "补贴金额", notes = "政府商务（终端客户）项目必须填写，根据补贴方式设置")
    private BigDecimal allowanceAmountOfMoney;

    @ApiModelProperty("计划安装人数")
    private Integer planInstallPeoples;

    @ApiModelProperty("计划年度")
    private String planYear;

    @ApiModelProperty("允许的能源类型")
    private List<String> allowEnergyTypes;

    @ApiModelProperty("允许的设备能源分类范围")
    private List<String> allowDeviceEnergyTypes;

    @ApiModelProperty("允许的设备品牌分类范围")
    private List<String> allowDeviceBrandTypes;

    @ApiModelProperty("允许的设备型号范围")
    private List<OpportunityDeviceModelDTO> allowDeviceModels;


    // ----------- 零售和终端商务项目参数 -----------

    @ApiModelProperty(value = "个人缴纳金额", notes = "设备总金额-补贴金额")
    private BigDecimal personalPayment;

    @ApiModelProperty(value = "是否安装光伏")
    private Boolean installPhotovoltaic;

    @ApiModelProperty(value = "申报类别")
    private DeclareCategoryEnum categoryOfDeclaration;

    @ApiModelProperty(value = "新申报面积")
    private BigDecimal newlyDeclaredArea;

    @ApiModelProperty(value = "原申报面积")
    private BigDecimal originalDeclaredArea;

    @ApiModelProperty(value = "客户编号-电表号/燃气表号")
    private String meterNumber;

    @ApiModelProperty("能源类型")
    private EnergyTypeEnum energyType;

    @ApiModelProperty(value = "门牌号")
    private String houseNumber;

    @ApiModelProperty(value = "户籍情况：01-本地，02-非本地")
    private String householdRegistration;

    @ApiModelProperty(value = "设备数量")
    private Integer equiNumber;

    @ApiModelProperty(value = "建筑面积")
    private BigDecimal projectArea;

    @ApiModelProperty(value = "建筑性质")
    private String buildNature;

    @ApiModelProperty(value = "建筑种类")
    private String bulidType;

    //  建筑信息
    @ApiModelProperty("是否农村")
    private String isVillage;

    @ApiModelProperty("房屋类型")
    private String houseType;

    @ApiModelProperty("采暖面积")
    private BigDecimal heatingArea;

    @ApiModelProperty("常用采暖面积")
    private BigDecimal commonHeatingArea;

    @ApiModelProperty("建筑层高")
    private BigDecimal buildingHeight;

    @ApiModelProperty("建筑结构")
    private String buildingStructure;

    @ApiModelProperty("外墙厚度")
    private String outerWallThickness;

    @ApiModelProperty("外墙保温")
    private String outerWallKeepWarm;

    @ApiModelProperty("供暖方式")
    private String heatingMode;

    @ApiModelProperty("机组安装位置")
    private String installPosition;

    @ApiModelProperty("房屋用途")
    private String housePerpose;

    @ApiModelProperty("房屋间数")
    private BigDecimal roomCount;

    @ApiModelProperty("图纸编号")
    private String drawingNo;

    @ApiModelProperty("设计单位")
    private String designCompany;

    @ApiModelProperty("窗户类型")
    private String windowType;

    @ApiModelProperty("设计热负荷")
    private BigDecimal designHeatingLoad;

    @ApiModelProperty("房屋朝向")
    private String houseDirection;

    @ApiModelProperty("末端改造状态")
    private String endState;

    @ApiModelProperty("采暖末端")
    private String heatingEnd;

    @Deprecated
    public SaveOpportunityBO convertToBo() {
        SaveOpportunityBO bo = new SaveOpportunityBO();
        BeanUtils.copyProperties(this, bo);
        if (StrUtil.isNotEmpty(opportunityClassification)) {
            bo.setOpportunityClassification(OpportunityClassificationEnum.getEnum(opportunityClassification));
        }
        if (StrUtil.isNotEmpty(opportunityStage)) {
            bo.setOpportunityStage(OpportunityStageEnum.getEnum(opportunityStage));
        }
        if (StrUtil.isNotEmpty(opportunityType)) {
            bo.setOpportunityType(OpportunityTypeEnum.getEnum(opportunityType));
        }
        if (StrUtil.isNotEmpty(personInCharge)) {
            bo.setPersonInCharge(personInCharge);
        }
        if (StrUtil.isNotEmpty(personInChargePhone)) {
            bo.setPersonInChargePhone(personInChargePhone);
        }

        return bo;
    }

    public OpportunityBO convertToOpportunityBO() {
        OpportunityBO bo = new OpportunityBO();
        BeanUtils.copyProperties(this, bo);
        bo.setProjectRelationDataTypeEnum(ProjectRelationDataTypeEnum.MANUAL_OPERATION);
        if (StrUtil.isEmpty(uuid)) {
            bo.setAddOpportunity(Boolean.TRUE);
        }
        if (StrUtil.isNotEmpty(opportunityClassification)) {
            bo.setOpportunityClassification(OpportunityClassificationEnum.getEnum(opportunityClassification));
        }
        if (StrUtil.isNotEmpty(opportunityStage)) {
            bo.setOpportunityStage(OpportunityStageEnum.getEnum(opportunityStage));
        }
        if (StrUtil.isNotEmpty(opportunityType)) {
            bo.setOpportunityType(OpportunityTypeEnum.getEnum(opportunityType));
        }
        bo.setCategoryOfDeclaration(categoryOfDeclaration);
        bo.setCustomerUuid(customerId);
        bo.setAreaCode(xzqhdm);
        bo.setEnergyType(energyType);
        if (OpportunityClassificationEnum.GOVERNMENT_PUBLIC.equals(bo.getOpportunityClassification())) {
            bo.setAllowAreaCodes(allowAreaCodes);
            bo.setEnergyTypes(allowEnergyTypes);
            bo.setAllowDeviceBrandTypes(allowDeviceBrandTypes);
            bo.setAllowDeviceEnergyTypes(allowDeviceEnergyTypes);
            bo.setAllowDeviceModels(allowDeviceModels);
        } else {
            bo.setAllowAreaCodes(Collections.singletonList(new OpportunityAreaCodeDTO(xzqhdm)));
        }
        if (StrUtil.isNotEmpty(personInCharge)) {
            bo.setPersonInCharge(personInCharge);
        }
        if (StrUtil.isNotEmpty(personInChargePhone)) {
            bo.setPersonInChargePhone(personInChargePhone);
        }
        return bo;
    }

}
